package com.yy.micro.magic.ext.domain.code;

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeMapProvider {
    private String planCode;

    private List<CodeMap> codeMaps;

    @JsonIgnore
    private Map<String,CodeMap> codeMapMap;

    public String getPlanCode() {
        return planCode;
    }

    public void setPlanCode(final String planCode) {
        this.planCode = planCode;
    }

    public List<CodeMap> getCodeMaps() {
        return codeMaps;
    }

    public void setCodeMaps(final List<CodeMap> codeMaps) {
        this.codeMaps = codeMaps;
        if (!CollectionUtils.isEmpty(this.codeMaps)){
            this.codeMapMap = new HashMap<>();
            codeMaps.stream().forEach(c->{
                codeMapMap.put(c.getName(),c);
            });
        }
    }

    @JsonIgnore
    public Object findCodeTargetValue(String name,Object value){
        return findCodeTargetValue(name,value,value);
    }
    @JsonIgnore
    public Object findCodeTargetValue(String name,Object value,Object defaultValue){
        if (!CollectionUtils.isEmpty(this.codeMapMap)&& this.codeMapMap.containsKey(name)){
            CodeMap code = this.codeMapMap.get(name);
            return code.findTargetValue(value,defaultValue);
        }
        return defaultValue;
    }
    @JsonIgnore
    public CodeMapItem findCodeMapItem(String name,Object value){
        if (!CollectionUtils.isEmpty(this.codeMapMap)&& this.codeMapMap.containsKey(name)){
            CodeMap code = this.codeMapMap.get(name);
            return code.findCodeItem(value);
        }
        return null;
    }
}
